CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/next/pages/share/public_paths/embed/[...id].tsx
Views: 687
1
/*
2
* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
3
* License: MS-RSL – see LICENSE.md for details
4
*/
5
6
import { Alert } from "antd";
7
import Link from "next/link";
8
import PathContents from "components/share/path-contents";
9
import PathActions from "components/share/path-actions";
10
import Loading from "components/share/loading";
11
import getPublicPathInfo from "lib/share/get-public-path-info";
12
import useCounter from "lib/share/counter";
13
import { Embed } from "components/share/layout";
14
import withCustomize from "lib/with-customize";
15
import { Customize } from "lib/share/customize";
16
import { getTitle } from "lib/share/util";
17
import { Customize as CustomizeType } from "@cocalc/database/settings/customize";
18
import { PathContents as PathContentsInterface } from "lib/share/get-contents";
19
20
interface Props {
21
id: string;
22
project_id: string;
23
path: string;
24
relativePath: string;
25
contents: PathContentsInterface;
26
error: string;
27
customize: CustomizeType;
28
}
29
30
export default function PublicPath(props: Props) {
31
const { id, project_id, path, relativePath, contents, error, customize } =
32
props;
33
useCounter(id);
34
if (id == null) return <Loading style={{ fontSize: "30px" }} />;
35
36
return (
37
<Customize value={customize}>
38
<Embed title={getTitle({ path, relativePath })}>
39
<div
40
style={{
41
backgroundColor: "white",
42
display: "inline-block",
43
padding: "0 5px",
44
margin: "5px",
45
width: "100%",
46
}}
47
>
48
<PathActions
49
id={id}
50
path={path}
51
project_id={project_id}
52
relativePath={relativePath}
53
isDir={!!contents?.isdir}
54
exclude={new Set(["embed"])}
55
/>
56
</div>
57
{contents != null && (
58
<PathContents
59
id={id}
60
relativePath={relativePath}
61
path={path}
62
{...contents}
63
/>
64
)}
65
{error != null && (
66
<Alert
67
showIcon
68
type="error"
69
style={{ maxWidth: "700px", margin: "30px auto" }}
70
message="Error loading file"
71
description={
72
<div>
73
There was a problem loading "{relativePath}" in{" "}
74
<Link href={`/share/public_paths/${id}`}>{path}.</Link>
75
<br />
76
<br />
77
{error}
78
</div>
79
}
80
/>
81
)}
82
</Embed>
83
</Customize>
84
);
85
}
86
87
export async function getServerSideProps(context) {
88
const id = context.params.id[0];
89
const relativePath = context.params.id.slice(1).join("/");
90
try {
91
const props = await getPublicPathInfo({
92
id,
93
relativePath,
94
req: context.req,
95
});
96
return await withCustomize({
97
context,
98
props: { ...props, layout: "embed" },
99
});
100
} catch (_err) {
101
console.log(_err);
102
return { notFound: true };
103
}
104
}
105
106